//package org.hefan.hrm.feign.strategy;
//
//import com.netflix.hystrix.HystrixThreadPoolKey;
//import com.netflix.hystrix.HystrixThreadPoolProperties;
//import com.netflix.hystrix.strategy.HystrixPlugins;
//import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
//import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable;
//import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
//import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
//import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
//import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
//import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
//import com.netflix.hystrix.strategy.properties.HystrixProperty;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//
//import java.util.concurrent.BlockingQueue;
//import java.util.concurrent.Callable;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
//@Configuration
//public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
//
//    private HystrixConcurrencyStrategy hystrixConcurrencyStrategy;
//
//    public FeignHystrixConcurrencyStrategy() {
//        try {
//            this.hystrixConcurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
//            if (this.hystrixConcurrencyStrategy instanceof FeignHystrixConcurrencyStrategy) {
//                return;
//            }
//            HystrixCommandExecutionHook commandExecutionHook =
//                    HystrixPlugins.getInstance().getCommandExecutionHook();
//            HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
//            HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
//            HystrixPropertiesStrategy propertiesStrategy =
//                    HystrixPlugins.getInstance().getPropertiesStrategy();
//
//            HystrixPlugins.reset();
//            HystrixPlugins instance = HystrixPlugins.getInstance();
//            instance.registerConcurrencyStrategy(this);
//            instance.registerCommandExecutionHook(commandExecutionHook);
//            instance.registerEventNotifier(eventNotifier);
//            instance.registerMetricsPublisher(metricsPublisher);
//            instance.registerPropertiesStrategy(propertiesStrategy);
//        } catch (Exception e) {
//            System.out.println("策略注册失败");
//        }
//    }
//
//    @Override
//    public <T> Callable<T> wrapCallable(Callable<T> callable) {
//        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//        return new WrappedCallable<>(callable, requestAttributes);
//    }
//
//    @Override
//    public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
//                                            HystrixProperty<Integer> corePoolSize,
//                                            HystrixProperty<Integer> maximumPoolSize,
//                                            HystrixProperty<Integer> keepAliveTime,
//                                            TimeUnit unit, BlockingQueue<Runnable> workQueue) {
//        return this.hystrixConcurrencyStrategy.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime,
//                unit, workQueue);
//    }
//
//    @Override
//    public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
//                                            HystrixThreadPoolProperties threadPoolProperties) {
//        return this.hystrixConcurrencyStrategy.getThreadPool(threadPoolKey, threadPoolProperties);
//    }
//
//    @Override
//    public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
//        return this.hystrixConcurrencyStrategy.getBlockingQueue(maxQueueSize);
//    }
//
//    @Override
//    public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
//        return this.hystrixConcurrencyStrategy.getRequestVariable(rv);
//    }
//
//    static class WrappedCallable<T> implements Callable<T> {
//        private final Callable<T> target;
//        private final RequestAttributes requestAttributes;
//
//        public WrappedCallable(Callable<T> target, RequestAttributes requestAttributes) {
//            this.target = target;
//            this.requestAttributes = requestAttributes;
//        }
//
//        @Override
//        public T call() throws Exception {
//            try {
//                RequestContextHolder.setRequestAttributes(requestAttributes);
//                return target.call();
//            } finally {
//                RequestContextHolder.resetRequestAttributes();
//            }
//        }
//    }
//}